﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TBB14 : ViewBase
    {
        public string FROMDATE = "", ENDDATE = "", ZH = "";
         

        #region ==dwBW===========
        public static readonly DependencyProperty dwBWProperty = DependencyProperty.Register("dwBW",
            typeof(DataRow), typeof(TBB14),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwBWChanged));
        public DataRow dwBW
        {
            get
            {
                return (DataRow)GetValue(dwBWProperty);
            }
            set
            {
                SetValue(dwBWProperty, value);
            }
        }
        private static void OndwBWChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TBB14;
            if (src != null)
            {
                src.dwBW = (DataRow)e.NewValue;
            }
        }
        #endregion

        #region ==AUMLFD===========
        public static readonly DependencyProperty AUMLFDProperty = DependencyProperty.Register("AUMLFD",
            typeof(string), typeof(TBB14),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUMLFDChanged));
        public string AUMLFD
        {
            get
            {
                return (string)GetValue(AUMLFDProperty);
            }
            set
            {
                SetValue(AUMLFDProperty, value); 
            }
        }
        private static void OnAUMLFDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TBB14;
            if (src != null)
            {
                src.AUMLFD = (string)e.NewValue;
            }
        }
        #endregion

        #region ==TLP===========
        public static readonly DependencyProperty TLPProperty = DependencyProperty.Register("TLP",
            typeof(string), typeof(TBB14),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTLPChanged));
        public string TLP
        {
            get
            {
                return (string)GetValue(TLPProperty);
            }
            set
            {
                SetValue(TLPProperty, value);
                if (dwBW != null)
                {
                    SourceLP = dwBW["BMLP"].ToString();
                    dwBW["BMLP"] = value;
                }
            }
        }
        private static void OnTLPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TBB14;
            if (src != null)
            {
                src.TLP = (string)e.NewValue;
            }
        }
        #endregion
        ITXEXRPT ITXEXRPT;

        public TBB14()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VBABEW();
            this.DataContext = MV;
            InitializeComponent();
           
        }
        string SourceLP = "";
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwBW = dwMaster;
            if (dwBW == null)
            {
                AUMLFD = "";
            }
            else
            { 
                AUMLFD = dwBW["BBBAUMKRS"].ToString() + dwBW["BBBAUMLFD"].ToString();
            } 
        }

        private void txtZH_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                QueryCommand(null, null);
            }
        }

        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            DataSet Result;
            DataSet Return = new DataSet();
            DataRow dwData = null;
            ZH = txtZH.Text.ToString().Trim();
            if (FROMDATE == "")
            {
                FROMDATE = "20000000";
            }
            if (ENDDATE == "")
            {
                ENDDATE = "99999999";
            }
            if (ZH == "")
            {
                ZH = "000000";
            }

            dwData = MV.SetProd.Tables["BABEW"].NewRow();

            dwData["BBBAUMLFD"] = ZH;
            dwData["BBWERTDAT"] = FROMDATE;
            dwData["BBBUCHDAT"] = ENDDATE;
            try
            {
                Result = ((VBABEW)MV).QueryBABEW(dwData, 5);
                MergeData(Result, true, true);
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
    

        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                FROMDATE = "";
                FROMDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
         
        private void PrintCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ITXEXRPT == null)
                {
                    ITXEXRPT = new ITXEXRPT();
                    ITXEXRPT.Root = "C:\\IntexReport";
                    ITXEXRPT.strCompany = app.company;
                    ITXEXRPT.PrintType = "OpenExcel";
                }
                ITXEXRPT.dsSource = new DataSet();
                ITXEXRPT.dsSource.Merge(MV.SetProd);
                //ITXEXRPT.dsSource.Merge(Se.Env);

                ITXEXRPT.DocType = "JLTBB14";

                ITXEXRPT.strExcelFile = @"JLT\JLTREPORT.xls";
                string filename = ITXEXRPT.WriteExcelReport();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ENDDATE = "";
                ENDDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            
            cBB.SaveCommand(null, null);

            string TargLP = "";
            TargLP = cBB.dwBB["BMLP"].ToString();

            cBB.dwBB["BMLP"] = SourceLP;
            VBABEW.SVWB.NWBBEW(cBB.dwBB, "20", RUNID);//增加留位移动记录

            cBB.dwBB["BMLP"] = TargLP;
            VBABEW.SVWB.NWBBEW(cBB.dwBB, "21", RUNID);//增加留位移动记录
        } 
    }
}
